new Vue({
  el: "#app",
  data() {
    return {
      pickerOptions: pickerOptions,
      form: {
        System_SerialNumber: "",
        System_Name: "",
        Picture_URL: "",
        System_Remark: "",
      },
      fileList: [],
      fmfileList: [],
      loading: false,
    };
  },
  mounted() {
    const Enterprise_Info_SerialNumber = sessionStorage.getItem(
      "Enterprise_Info_SerialNumber"
    );
    this.getData(Enterprise_Info_SerialNumber);
  },
  methods: {
    handleRemove2(file, fileList) {
      this.fileList = [];
    },
    handleRemove(file, fileList) {
      this.fmfileList = [];
    },
    getData(Enterprise_Info_SerialNumber) {
      const that = this;
      $.ajax({
        headers: requestHeaders(),
        url: `${BASE_URL2}/generalBusiness/generalQueryByTableNameWithOriginal`,
        type: "POST",
        data: {
          Data: "enterprise_info",
          Condition:
            "Enterprise_Info_SerialNumber='" +
            Enterprise_Info_SerialNumber +
            "'",
        },
        success: function (data) {
          console.log(data);

          if (data.length == 1) {
            console.log(data[0]);
            console.log(data[0].Enterprise_Logo);

            let lArray_URL = new Array();
            let lArray_URL2 = new Array();
            let lJSONObject_Enterprise_Logo = new Object();
            let lJSONObject_Picture_Record = new Object();
            lJSONObject_Enterprise_Logo.url = `https://www.kdddy.cn/Image/${data[0].Enterprise_Logo}`;
            lJSONObject_Picture_Record.url = `https://www.kdddy.cn/Image/${data[0].Picture_URL}`;
            lArray_URL.push(lJSONObject_Enterprise_Logo);
            lArray_URL2.push(lJSONObject_Picture_Record);
            that.fmfileList = lArray_URL;
            that.fileList = lArray_URL2;
            that.form = data[0];
          }
        },
        complete: function (XMLHttpRequest) {
          requestPretreatment(XMLHttpRequest); //config/config.js 响应预检
        },
        dataType: "json",
      });
    },
    handleRemove(file, fileList) {
      // this.fileList = [];
      this.fmfileList.splice(0, 1);
    },
    submitForm(formName) {
      const that = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          that.loading = true;
          if (that.fmfileList.length > 0){
            let base64Image = that.fmfileList[0].url;
            let dataIndex = base64Image.indexOf("data:");
            let base64Index = base64Image.indexOf(";base64,");
            let substring = base64Image.substring(dataIndex + 5, base64Index);
            let ls_split = substring.split("/");
            that.form.Enterprise_Logo =
              that.fmfileList[0].uid + "." + ls_split[1];
          } else {
            that.form.Enterprise_Logo = '';
          }

          if (that.fileList.length > 0) {
            let base64Image2 = that.fileList[0].url;
            let dataIndex2 = base64Image2.indexOf("data:");
            let base64Index2 = base64Image2.indexOf(";base64,");
            let substring2 = base64Image2.substring(
              dataIndex2 + 5,
              base64Index2
            );
            let ls_split2 = substring2.split("/");
            that.form.Picture_URL = that.fileList[0].uid + "." + ls_split2[1];
          } else {
            that.form.Picture_URL = '';
          }

          console.log(that.form);
          // return;
          $.ajax({
            headers: requestHeaders(),
            type: "POST",
            // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
            url: `${BASE_URL2}/generalBusiness/generalSave`,
            data: {
              Data: JSON.stringify({
                Modify: {
                  enterprise_info: [that.form],
                },
              }),
            },
            success: function (Result) {
              console.log(Result);
              window.history.go(-1);
            },
            complete: function (XMLHttpRequest) {
              //   requestPretreatment(XMLHttpRequest);
            },
            dataType: "json",
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    deal() {
      const modify = deepClone(this.form);
      return { add: null, modify };
    },
    chandleChange(file, fileList) {
      // 这个方法现在主要用于调试或处理其他上传后的逻辑
      // 注意：这里的fileList参数与:file-list绑定的fileList不是同一个对象
      // :file-list绑定的fileList是组件内部维护的，而这里的fileList是上传事件提供的
      console.log("Upload change event:", file, fileList);
    },
    handleBefileList(file) {
      const maxSize = 500 * 1024; // 500KB
      if (file.size > maxSize) {
        this.$message.error(
          "图片大小超出限制，最大允许上传 500KB 的图片，请压缩后重新上传"
        );
        return;
      }
      // 生成预览URL并添加到fileList中
      const reader = new FileReader();
      reader.onload = (e) => {
        const newFile = {
          name: file.name,
          url: e.target.result, // 预览URL
          uid: file.uid,
          // 可以添加其他属性，如上传状态等
        };
        this.fileList.push(newFile);
        console.log(this.fileList);

        $.ajax({
          // headers: requestHeaders(), // 确保这个函数返回正确的头部信息
          url: "https://service.kdddy.cn/donghuiForRequest/store/photo",
          // url: "http://192.168.10.79:8087/store/photo",
          // url: "http://192.168.10.79:8087/store/photo",
          type: "POST", // 注意HTTP方法应该大写
          contentType: "application/json", // 设置请求体的媒体类型为application/json
          data: JSON.stringify(this.fileList), // 将数据序列化为JSON字符串
          success: function (Result) {
            console.log(Result);
          },
          complete: function (XMLHttpRequest) {
            requestPretreatment(XMLHttpRequest); // 确保这个函数在config/config.js中定义
          },
          dataType: "json", // 预期服务器返回的数据类型
        });
      };
      reader.readAsDataURL(file);
      // console.log(this.fmfileList);

      // 返回false以阻止自动上传
      return false;
    },
    handleBeforeUpload(file) {
      const maxSize = 500 * 1024; // 500KB
      if (file.size > maxSize) {
        this.$message.error(
          "图片大小超出限制，最大允许上传 500KB 的图片，请压缩后重新上传"
        );
        return;
      }
      // 生成预览URL并添加到fileList中
      const reader = new FileReader();
      reader.onload = (e) => {
        const newFile = {
          name: file.name,
          url: e.target.result, // 预览URL
          uid: file.uid,
          // 可以添加其他属性，如上传状态等
        };
        this.fmfileList.push(newFile);
        console.log(this.fmfileList);

        $.ajax({
          // headers: requestHeaders(), // 确保这个函数返回正确的头部信息
          url: "https://service.kdddy.cn/donghuiForRequest/store/photo",
          // url: "http://192.168.10.79:8087/store/photo",
          // url: "http://192.168.10.79:8087/store/photo",
          type: "POST", // 注意HTTP方法应该大写
          contentType: "application/json", // 设置请求体的媒体类型为application/json
          data: JSON.stringify(this.fmfileList), // 将数据序列化为JSON字符串
          success: function (Result) {
            console.log(Result);
          },
          complete: function (XMLHttpRequest) {
            requestPretreatment(XMLHttpRequest); // 确保这个函数在config/config.js中定义
          },
          dataType: "json", // 预期服务器返回的数据类型
        });
      };
      reader.readAsDataURL(file);
      // console.log(this.fmfileList);

      // 返回false以阻止自动上传
      return false;
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    uploadFront() {
      let _this = this;
      let num = 1;
      new Compressor(file, {
        quality: num,
        success(file) {
          console.log(file.size);
          const formData = new FormData();
          formData.append("file", file, file.name);
          let oFileReader = new FileReader();
          oFileReader.readAsDataURL(file);
          oFileReader.onload = function (e) {
            $.ajax({
              type: "POST",
              url: DomainName + "/FileList",
              data: {
                Data: e.target.result,
                DataLength: file.size,
                FileName: file.name,
                Path: "icon",
              },
              complete: function (XMLHttpRequest) {
                _this.loading = false;
                if (
                  XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT"
                ) {
                  window.parent.document.location.href =
                    absolutepath() +
                    XMLHttpRequest.getResponseHeader("CONTENTPATH");
                }
              },
              success: function (Result) {
                _this.form.pic = e.target.result;
                if (Result.Status == "success") {
                  _this.form.Picture_URL = Result.Picture_Url;
                  _this.$message.success("上传成功");
                } else {
                  _this.$message.error("上传失败");
                }
              },
              error: function () {
                _this.$message.error("操作失败，请稍后再试");
              },
              dataType: "json",
            });
          };
        },
        error(err) {
          console.log(err.message);
        },
      });
    },
  },
});
